<?php

$dsn = "mysql:host=localhost;dbname=bhdw";
$username = 'root';
$password = '123456';
$table = 'sex8';
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8';");
$dbConnection = new PDO($dsn, $username, $password, $options);
$stmt = $dbConnection->query('SELECT * FROM ' . $table . ' WHERE pic_local_url <> "" ORDER BY id DESC');
$stmt2 = $dbConnection->prepare('UPDATE ' . $table . ' SET pic_local_url="",is_download=:is_download WHERE id=:id');
$index = 1;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    if (!file_exists($row['pic_local_url'])) {
        $stmt2->execute(array(':id' => $row['id'], ':is_download' => 0));
        echo $row['pic_local_url'] . "\n";
        $index++;
    } elseif (filesize($row['pic_local_url']) < 51200) {
        if (@getimagesize($row['pic_local_url']) !== FALSE) {//是一个图片
            $stmt2->execute(array(':id' => $row['id'], ':is_download' => 2));
            echo '分辨率太低';
        } else {
            $stmt2->execute(array(':id' => $row['id'], ':is_download' => 0));
            echo '非图片';
        }
        unlink($row['pic_local_url']);
//        echo filesize($row['pic_local_url']) . "\n";
//        var_dump(filetype($row['pic_local_url']));
//        exit;
//        $stmt2->execute(array(':id' => $row['id'], ':is_download' => 0));
        echo '[删除文件]' . $row['pic_local_url'] . "\n";
        $index++;
    } elseif ($row['is_download'] == 2) {
        $stmt2->execute(array(':id' => $row['id'], ':is_download' => 1));
        echo '[更改状态]' . $row['pic_local_url'] . "\n";
        $index++;
    }
}
echo '共修改了' . $index . '行' . "\n";
